/**
 * @description 分页通用对象定义
 * @author lifeng
 * @date 2007-07-07
 */
package com.mobile.framework.util;

import java.util.List;

public class PageInfo {
    private int page = 0; // 当前页码
    private int pageSize = 10; // 每页多少行
    private int count = 0; // 总记录数
    private int pageNum = 0; // 总页数 ok
    private String webView; // 前台分页样式串 ok
    private String adminView; // 后台分页样式串 ok
    private String simpleAdminView; // 后台分页样式串 ok

    @SuppressWarnings("unchecked")
    private List list; // 分页列表

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getPageNum() {
        // return pageNum;
        if (count % pageSize == 0) {
            pageNum = count / pageSize;
        } else {
            pageNum = count / pageSize + 1;
        }
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public String getWebView() {
        StringBuffer results = new StringBuffer();
        results.append("<div class='mes_next'><ul>");

        int pagescope = (int) Math.floor(page / 10);

        results.append("<li class=\"li_next\"><a href=\"javascript:goToPage("
                + (1) + ");\">第一页</a></li>");
        if (page > 1) {
            results
                    .append("<li class=\"li_next\"><a href=\"javascript:goToPage("
                            + (page - 1) + ");\">&lt;&lt;</a></li>");
        }
        for (int i = pagescope * 10; i <= (pagescope + 1) * 10; i++) {
            if (i == 0)
                continue;
            if (i <= getPageNum()) {
                if (i == page)
                    results.append("&nbsp;<li class=\"li_next\">" + i
                            + "</li>&nbsp;");
                else
                    results
                            .append("<li class=\"li_next\"><a href=\"javascript:goToPage("
                                    + i
                                    + ");\">"
                                    + i
                                    + "</a><li class=\"li_next\">");
            }
        }
        if (page < getPageNum()) {
            results
                    .append("<li class=\"li_next\"><a href=\"javascript:goToPage("
                            + (page + 1) + ");\">&gt;&gt;</a></li>");
        }
        results.append("<li class=\"li_next\"><a href=\"javascript:goToPage("
                + (pageNum) + ");\">最后一页</a></li>");
        results.append("<input value=" + page
                + " id=\"cursor\" name=\"cursor\" type=\"hidden\">");
        results.append("</div></ul>");
        return results.toString();
    }

    public String getWebView1() {
        StringBuffer results = new StringBuffer();
        results
                .append("<table id=paginate border=0 width=90% align=center class=font1 cellPadding=0 cellSpacing=0><tr><td align='right'>");
        results.append("共" + count + "条记录,");
        results.append("  分" + getPageNum() + "页 &nbsp;&nbsp;");

        int pagescope = (int) Math.floor(page / 10);
        if (page > 1) {
            results.append("<a href=\"javascript:goToPage(" + (page - 1)
                    + ");\"><b>上一页</b></a>");
        }
        for (int i = pagescope * 10; i <= (pagescope + 1) * 10; i++) {
            if (i == 0)
                continue;
            if (i <= getPageNum()) {
                if (i == page)
                    results.append("<font size=2 color=#000000><b>" + i
                            + "</b></font>");
                else
                    results.append("<a href=\"javascript:goToPage(" + i
                            + ");\">[" + i + "]</a>");
            }
        }
        if (page < getPageNum()) {
            results.append("<a href=\"javascript:goToPage(" + (page + 1)
                    + ");\"><b>下一页</b></a>");
        }
        results.append("<input value=" + page
                + " id=\"cursor\" name=\"cursor\" type=\"hidden\" size=\"4\">");
        results.append("</td></tr></table>");
        return results.toString();
    }

    public void setWebView(String webView) {
        this.webView = webView;
    }

    public String getAdminView() {
        if (page <= 1) {
            page = 1;
        }

        if (page >= getPageNum()) {
            page = getPageNum();
        }

        StringBuffer results = new StringBuffer();
        results
                .append("<table id=paginate border=0 width=90% align=center cellPadding=0 cellSpacing=0><tr><td align='center'>");
        results.append("共" + count + "条记录");
        results.append("  当前第" + page + "页/分" + getPageNum() + "页 ");

        if (page <= 1) {

            results.append("首页");
            results.append("&nbsp;");
            results.append("上一页");
        } else {
            results.append("<a href=\"javascript:goToPage(1);\">首页</a>");
            results.append("&nbsp;");
            results.append("<a href=\"javascript:goToPage(" + (page - 1)
                    + ");\">上一页</a>");
        }
        results.append("&nbsp;");
        if (page >= getPageNum()) {
            results.append("下一页");
            results.append("&nbsp;");
            results.append("末页");
            //results.append("<input value=" + getPageNum() + " id=\"cursor\" name=\"cursor\" type=\"hidden\" size=\"4\">");
            results.append("&nbsp;<select name='pageSize' onchange='this.form.submit();'>");
            for (int i = 1; i <= 200; i++) {
                results.append("<option value='" + i + "'" + this.choosePageSizeOption(i, pageSize) + ">" + i + "</option>");
            }
            results.append("</select>");
            results.append("<input value=" + getPageNum() + " id=\"cursor\" name=\"cursor\" type=\"text\" size=\"4\"><a href=\"#\" onclick=\"javascript:goToPage_1();\">GO</a>");

        } else {
            results.append("<a href=\"javascript:goToPage(" + (page + 1)
                    + ");\">下一页</a>");
            results.append("&nbsp;");
            results.append("<a href=\"javascript:goToPage(" + (pageNum)
                    + ");\">末页</a>");

            //results.append("<input value=" + page + " id=\"cursor\" name=\"cursor\" type=\"hidden\" size=\"4\">");
            results.append("&nbsp;<select name='pageSize' onchange='this.form.submit();'>");
            for (int i = 1; i <= 200; i++) {
                results.append("<option value='" + i + "'" + this.choosePageSizeOption(i, pageSize) + ">" + i + "</option>");
            }
            results.append("</select>");
            results.append("<input value=" + page + " id=\"cursor\" name=\"cursor\" type=\"text\" size=\"4\"><a href=\"#\" onclick=\"javascript:goToPage_1();\">GO</a>");

        }
        results.append("</td></tr></table>");
        return results.toString();
    }

    public String getSimpleAdminView() {
        StringBuffer results = new StringBuffer();
        results
                .append("<table id=paginate border=0 width=90% align=center class=font1 cellPadding=0 cellSpacing=0><tr><td align='center'>");

        //results.append("共" + count + "条记录");
        //results.append("  当前第" + page + "页/分" + getPageNum() + "页 ");

        if (page <= 1) {
            results.append("首页");
            results.append("&nbsp;");
            results.append("上一页");
        } else {
            results.append("<a href=\"javascript:goToPage(1);\">首页</a>");
            results.append("&nbsp;");
            results.append("<a href=\"javascript:goToPage(" + (page - 1)
                    + ");\">上一页</a>");
        }
        results.append("&nbsp;");
        if (page >= getPageNum()) {
            results.append("下一页");
            results.append("&nbsp;");
            results.append("末页");
            results.append("<input value=" + getPageNum() + " id=\"cursor\" name=\"cursor\" type=\"hidden\" size=\"4\">");

        } else {
            results.append("<a href=\"javascript:goToPage(" + (page + 1)
                    + ");\">下一页</a>");
            results.append("&nbsp;");
            results.append("<a href=\"javascript:goToPage(" + (pageNum)
                    + ");\">末页</a>");

            results.append("<input value=" + page + " id=\"cursor\" name=\"cursor\" type=\"hidden\" size=\"4\">");

        }
        results.append("</td></tr></table>");
        return results.toString();
    }

    public void setSimpleAdminView(String simpleAdminView) {
        this.simpleAdminView = simpleAdminView;
    }

    public void setAdminView(String adminView) {
        this.adminView = adminView;
    }

    @SuppressWarnings("unchecked")
    public List getList() {
        return list;
    }

    @SuppressWarnings("unchecked")
    public void setList(List list) {
        this.list = list;
    }

    private String choosePageSizeOption(int currentPageSize, int pageSize) {
        if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else if (currentPageSize == pageSize) {
            return " selected";
        } else {
            return "";
        }
    }
}
